package com.fr.dialog.core;

import com.fr.base.BaseUtils;
import com.fr.base.Inter;
import com.fr.cell.core.layout.LayoutFactory;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.GridLayout;
import java.awt.Insets;
import java.awt.Point;
import java.awt.Window;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JSeparator;
import javax.swing.JWindow;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.border.AbstractBorder;
import javax.swing.border.LineBorder;
import javax.swing.event.AncestorEvent;
import javax.swing.event.AncestorListener;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

public class ColorSelectionPane extends JPanel
{
  private Color color;
  private boolean isCanBeNull;
  private JComponent displayComponent;
  private JButton triggleButton;
  private ColorControlWindow colorSetPopupWindow;
  private List changeListenerList;
  private final Color[] menuColors;
  private final Color[] menuColors2;

  public ColorSelectionPane(int paramInt)
  {
    this(paramInt, 24, 16);
  }

  public ColorSelectionPane(int paramInt1, int paramInt2, int paramInt3)
  {
    this.color = null;
    this.isCanBeNull = false;
    this.changeListenerList = new ArrayList();
    this.menuColors = new Color[] { Color.black, new Color(153, 51, 0), new Color(51, 51, 0), new Color(0, 51, 0), new Color(0, 51, 102), new Color(0, 0, 128), new Color(51, 51, 153), new Color(51, 51, 51), new Color(128, 0, 0), new Color(252, 102, 0), new Color(128, 128, 0), new Color(0, 128, 0), new Color(0, 128, 128), new Color(0, 0, 255), new Color(102, 102, 153), new Color(128, 128, 128), new Color(255, 0, 0), new Color(255, 153, 0), new Color(153, 204, 0), new Color(51, 153, 102), new Color(51, 204, 204), new Color(51, 102, 255), new Color(128, 0, 128), new Color(150, 150, 150), new Color(255, 0, 255), new Color(255, 204, 0), new Color(255, 255, 0), new Color(0, 255, 0), new Color(0, 255, 255), new Color(0, 204, 255), new Color(153, 51, 102), new Color(192, 192, 192), new Color(255, 153, 204), new Color(255, 204, 153), new Color(255, 255, 153), new Color(204, 255, 204), new Color(204, 255, 255), new Color(153, 204, 255), new Color(204, 153, 255), new Color(255, 255, 255) };
    this.menuColors2 = new Color[] { new Color(153, 153, 255), new Color(153, 51, 102), new Color(255, 255, 204), new Color(204, 255, 255), new Color(102, 0, 102), new Color(255, 128, 128), new Color(0, 102, 204), new Color(204, 204, 255), new Color(0, 0, 128), new Color(255, 0, 255), new Color(255, 255, 0), new Color(0, 255, 255), new Color(128, 0, 128), new Color(128, 0, 0), new Color(0, 128, 128), new Color(0, 0, 255) };
    setLayout(LayoutFactory.createBorderLayout());
    this.displayComponent = new JLabel();
    this.displayComponent.setBorder(BorderFactory.createLineBorder(Color.gray));
    this.triggleButton = new JButton(BaseUtils.readIcon("/com/fr/dialog/images/color/popupCenter.png"));
    this.triggleButton.setBorder(new TriggleLineBorder(this));
    JPanel localJPanel = new JPanel();
    localJPanel.setLayout(new BorderLayout(0, 0));
    localJPanel.add(this.displayComponent, "Center");
    localJPanel.setOpaque(true);
    this.displayComponent.setOpaque(true);
    this.displayComponent.addMouseListener(new PopupMouseListener(this));
    this.triggleButton.addMouseListener(new PopupMouseListener(this));
    add(localJPanel, "Center");
    add(this.triggleButton, "East");
    Dimension localDimension1 = new Dimension(paramInt3, paramInt2);
    this.triggleButton.setMaximumSize(localDimension1);
    this.triggleButton.setMinimumSize(localDimension1);
    this.triggleButton.setPreferredSize(localDimension1);
    this.triggleButton.setSize(localDimension1);
    Dimension localDimension2 = new Dimension(Math.max(paramInt1, localJPanel.getPreferredSize().width), paramInt2);
    localJPanel.setMaximumSize(localDimension2);
    localJPanel.setMinimumSize(localDimension2);
    localJPanel.setPreferredSize(localDimension2);
    localJPanel.setSize(localDimension2);
    addAncestorListener(new AncestorListener(this)
    {
      private final ColorSelectionPane this$0;

      public void ancestorAdded()
      {
      }

      public void ancestorRemoved()
      {
        ColorSelectionPane.access$000(this.this$0);
      }

      public void ancestorMoved()
      {
        ColorSelectionPane.access$000(this.this$0);
      }
    });
  }

  public Color getColor()
  {
    if (!(isEnabled()))
      return null;
    return this.color;
  }

  public void setColor(Color paramColor)
  {
    ChangeEvent localChangeEvent;
    int i;
    this.color = paramColor;
    if ((paramColor == null) || (paramColor.getAlpha() == 0))
      this.displayComponent.setBackground(null);
    else
      this.displayComponent.setBackground(paramColor);
    if (this.changeListenerList.size() > 0)
    {
      localChangeEvent = new ChangeEvent(this);
      for (i = 0; i < this.changeListenerList.size(); ++i)
        ((ChangeListener)this.changeListenerList.get(i)).stateChanged(localChangeEvent);
    }
    this.displayComponent.repaint();
  }

  public boolean isCanBeNull()
  {
    return this.isCanBeNull;
  }

  public void setCanBeNull(boolean paramBoolean)
  {
    this.isCanBeNull = paramBoolean;
  }

  public void setEnabled(boolean paramBoolean)
  {
    super.setEnabled(paramBoolean);
    this.displayComponent.setEnabled(paramBoolean);
    this.triggleButton.setEnabled(paramBoolean);
  }

  private void showPopupMenu()
  {
    if ((this.colorSetPopupWindow != null) && (this.colorSetPopupWindow.isVisible()))
    {
      hidePopupMenu();
      return;
    }
    if (!(isEnabled()))
      return;
    this.colorSetPopupWindow = getColorControlWindow();
    Point localPoint = new Point(0, 0);
    SwingUtilities.convertPointToScreen(localPoint, this);
    this.colorSetPopupWindow.setLocation(localPoint.x, localPoint.y + getSize().height);
    this.colorSetPopupWindow.setVisible(true);
  }

  private void hidePopupMenu()
  {
    if (this.colorSetPopupWindow != null)
      this.colorSetPopupWindow.setVisible(false);
    this.colorSetPopupWindow = null;
  }

  private ColorControlWindow getColorControlWindow()
  {
    if (this.colorSetPopupWindow == null)
    {
      Window localWindow = SwingUtilities.windowForComponent(this);
      if (localWindow != null)
        this.colorSetPopupWindow = new ColorControlWindow(this, localWindow, isCanBeNull());
    }
    return this.colorSetPopupWindow;
  }

  public void addColorChangeListener(ChangeListener paramChangeListener)
  {
    this.changeListenerList.add(paramChangeListener);
  }

  static void access$000(ColorSelectionPane paramColorSelectionPane)
  {
    paramColorSelectionPane.hidePopupMenu();
  }

  static void access$100(ColorSelectionPane paramColorSelectionPane)
  {
    paramColorSelectionPane.showPopupMenu();
  }

  static Color[] access$200(ColorSelectionPane paramColorSelectionPane)
  {
    return paramColorSelectionPane.menuColors;
  }

  static Color[] access$300(ColorSelectionPane paramColorSelectionPane)
  {
    return paramColorSelectionPane.menuColors2;
  }

  class TriggleLineBorder extends AbstractBorder
  {
    protected Insets borderInsets;
    private final ColorSelectionPane this$0;

    TriggleLineBorder()
    {
      this.this$0 = paramColorSelectionPane;
      this.borderInsets = new Insets(1, 0, 1, 0);
    }

    public void paintBorder(, Graphics paramGraphics, int paramInt1, int paramInt2, int paramInt3, int paramInt4)
    {
      paramGraphics.translate(paramInt1, paramInt2);
      paramGraphics.setColor(Color.gray);
      paramGraphics.drawLine(0, 0, paramInt3, 0);
      paramGraphics.drawLine(0, paramInt4 - 1, paramInt3, paramInt4 - 1);
      paramGraphics.drawLine(paramInt3 - 1, 0, paramInt3 - 1, paramInt4);
      paramGraphics.translate(-paramInt1, -paramInt2);
    }

    public Insets getBorderInsets()
    {
      return this.borderInsets;
    }
  }

  private class ColorCell extends JComponent
  {
    private Color color;
    private final ColorSelectionPane this$0;

    public ColorCell()
    {
      this.this$0 = paramColorSelectionPane;
      this.color = Color.white;
      enableEvents(16L);
      setPreferredSize(new Dimension(14, 14));
      setBorder(new LineBorder(Color.gray));
      setCursor(new Cursor(12));
    }

    public ColorCell(, Color paramColor)
    {
      this(paramColorSelectionPane);
      setColor(paramColor);
    }

    public void setColor()
    {
      this.color = paramColor;
      repaint();
    }

    public Color getColor()
    {
      return this.color;
    }

    public void paintComponent()
    {
      Dimension localDimension = getSize();
      Insets localInsets = getInsets();
      int i = localDimension.width - localInsets.left - localInsets.right;
      int j = localDimension.height - localInsets.top - localInsets.bottom;
      if (this.color == null)
      {
        paramGraphics.setColor(Color.white);
        paramGraphics.fillRect(localInsets.left, localInsets.top, localDimension.width - localInsets.left - localInsets.right, localDimension.height - localInsets.top - localInsets.bottom);
        paramGraphics.setColor(Color.gray);
        paramGraphics.drawLine(localInsets.left, localInsets.top, i, j);
        paramGraphics.drawLine(i, localInsets.top, localInsets.left, j);
      }
      else
      {
        paramGraphics.setColor(this.color);
        paramGraphics.fillRect(localInsets.left, localInsets.top, i, j);
      }
      if ((this.this$0.getColor() != null) && (this.this$0.getColor().equals(getColor())))
      {
        paramGraphics.setColor(Color.black);
        paramGraphics.drawRect(localInsets.left, localInsets.top, i - 1, j - 1);
      }
    }

    public void processMouseEvent()
    {
      if ((paramMouseEvent == null) || (paramMouseEvent.getID() == 502))
      {
        this.this$0.setColor(getColor());
        ColorSelectionPane.access$000(this.this$0);
      }
      if (paramMouseEvent != null)
        super.processMouseEvent(paramMouseEvent);
    }

    public Dimension getMinimumSize()
    {
      return getPreferredSize();
    }
  }

  class ColorSelectionPopupPane extends JPanel
  {
    private final ColorSelectionPane this$0;

    public ColorSelectionPopupPane(, boolean paramBoolean)
    {
      this.this$0 = paramColorSelectionPane;
      setLayout(new BorderLayout(0, 4));
      setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));
      if (paramBoolean)
      {
        localObject = new JButton(Inter.getLocText("Transparent"));
        ((JButton)localObject).setCursor(new Cursor(12));
        ((JButton)localObject).addActionListener(new TransparentActionListener(this));
        add((Component)localObject, "North");
      }
      Object localObject = new JPanel();
      add((Component)localObject, "Center");
      ((JPanel)localObject).setLayout(new BoxLayout((Container)localObject, 1));
      JPanel localJPanel1 = new JPanel();
      ((JPanel)localObject).add(localJPanel1);
      localJPanel1.setLayout(new GridLayout(5, 8, 5, 5));
      for (int i = 0; i < ColorSelectionPane.access$200(paramColorSelectionPane).length; ++i)
        localJPanel1.add(new ColorSelectionPane.ColorCell(paramColorSelectionPane, ColorSelectionPane.access$200(paramColorSelectionPane)[i]));
      ((JPanel)localObject).add(Box.createVerticalStrut(5));
      ((JPanel)localObject).add(new JSeparator());
      ((JPanel)localObject).add(Box.createVerticalStrut(5));
      JPanel localJPanel2 = new JPanel();
      ((JPanel)localObject).add(localJPanel2);
      localJPanel2.setLayout(new GridLayout(2, 8, 5, 5));
      for (int j = 0; j < ColorSelectionPane.access$300(paramColorSelectionPane).length; ++j)
        localJPanel2.add(new ColorSelectionPane.ColorCell(paramColorSelectionPane, ColorSelectionPane.access$300(paramColorSelectionPane)[j]));
      JButton localJButton = new JButton(Inter.getLocText("Custom") + "...");
      localJButton.setCursor(new Cursor(12));
      localJButton.addActionListener(new CustomActionListener(this));
      add(localJButton, "South");
    }

    static ColorSelectionPane access$400(ColorSelectionPopupPane paramColorSelectionPopupPane)
    {
      return paramColorSelectionPopupPane.this$0;
    }

    class CustomActionListener
      implements ActionListener
    {
      private final ColorSelectionPane.ColorSelectionPopupPane this$1;

      CustomActionListener()
      {
        this.this$1 = paramColorSelectionPopupPane;
      }

      public void actionPerformed()
      {
        ColorSelectionPane.access$000(ColorSelectionPane.ColorSelectionPopupPane.access$400(this.this$1));
        Color localColor = JColorChooser.showDialog(SwingUtilities.getWindowAncestor(ColorSelectionPane.ColorSelectionPopupPane.access$400(this.this$1)), "Select Color", ColorSelectionPane.ColorSelectionPopupPane.access$400(this.this$1).getColor());
        if (localColor != null)
          ColorSelectionPane.ColorSelectionPopupPane.access$400(this.this$1).setColor(localColor);
      }
    }

    class TransparentActionListener
      implements ActionListener
    {
      private final ColorSelectionPane.ColorSelectionPopupPane this$1;

      TransparentActionListener()
      {
        this.this$1 = paramColorSelectionPopupPane;
      }

      public void actionPerformed()
      {
        Color localColor = new Color(255, 255, 255, 0);
        ColorSelectionPane.ColorSelectionPopupPane.access$400(this.this$1).setColor(localColor);
        ColorSelectionPane.access$000(ColorSelectionPane.ColorSelectionPopupPane.access$400(this.this$1));
      }
    }
  }

  class ColorControlWindow extends JWindow
  {
    private final ColorSelectionPane this$0;

    public ColorControlWindow(, Window paramWindow, boolean paramBoolean)
    {
      super(paramWindow);
      this.this$0 = paramColorSelectionPane;
      initComponents(paramBoolean);
    }

    public void initComponents()
    {
      JPanel localJPanel = new JPanel();
      setContentPane(localJPanel);
      localJPanel.setBorder(UIManager.getBorder("PopupMenu.border"));
      localJPanel.setLayout(LayoutFactory.createBorderLayout());
      localJPanel.add(new ColorSelectionPane.ColorSelectionPopupPane(this.this$0, paramBoolean), "Center");
      pack();
    }
  }

  class PopupMouseListener extends MouseAdapter
  {
    private final ColorSelectionPane this$0;

    PopupMouseListener()
    {
      this.this$0 = paramColorSelectionPane;
    }

    public void mousePressed()
    {
      ColorSelectionPane.access$100(this.this$0);
    }
  }
}